<template>
	<view class='form'>
		<uni-forms ref="form" :rules="rules" :modelValue="formData">
			<uni-forms-item label="旧密码" name="oldPassword">
				<uni-easyinput v-model="formData.oldPwd" type="password" placeholder="请输入旧密码" />
			</uni-forms-item>
			<uni-forms-item label="新密码" name="newPassword">
				<uni-easyinput v-model="formData.pwd" type="password" placeholder="请输入新密码" />
			</uni-forms-item>
			<uni-forms-item label="确认密码" name="conPassword">
				<uni-easyinput v-model="formData.confirmPwd" type="password" placeholder="请再次输入" />
			</uni-forms-item>
		</uni-forms>
		<view style="margin: 16px">
			<van-button round block type="info" @click="submit"> 确定 </van-button>
		</view>
	</view>
</template>

<script>
	import {changePassword} from '@/api/changePassword/index.js'
	export default {
		data() {
			return {
				rules: {
					oldPassword: {
						rules: [{
								validateFunction: (rule, value, data, callback)=> {
									const psd=uni.getStorageSync('psd')
									if(value!==psd){
										callback('请输入正确的密码')
									}
									return true
								}
							},
							{
								required: true,
								errorMessage: '请填写旧密码'
							}
						],
						validateTrigger:"bind"
					},
					newPassword:{
						rules:[{
							validateFunction: (rule, value, data, callback)=> {
								const psd=uni.getStorageSync('psd')
								if(value===psd){
									callback('新密码不能与旧密码相同')
								}else if(!/^\d{6,}$/.test(value)){
									callback('密码由数字组成,密码长度要大于6位')
								}
								return true
							}
						},
						{
							required: true,
							errorMessage: '请填写新密码'
						}
						],
						validateTrigger:"bind"
					},
					conPassword:{
						rules:[{
							validateFunction: (rule, value, data, callback)=> {
								const psd=uni.getStorageSync('psd')
								if(value===psd){
									callback('新密码不能与旧密码相同')
								}else if(!/^\d{6,}$/.test(value)){
									callback('密码由数字组成,密码长度要大于6位')
								}else if(value!==this.formData.pwd){
									callback('与新密码不一致')
								}
								return true
							}
						},
						{
							required: true,
							errorMessage: '请再次填写'
						}
						],
						validateTrigger:"bind"
					}
				},
				formData: {
					oldPwd: '',
					pwd:'',
					confirmPwd:''
				}
			}
		},
		computed:{
			// rules(){
			// 	return {}
			// }
		},
		methods: {
			
			 submit(form) {
			            this.$refs.form.validate().then(res=>{
							uni.showLoading({
							    title: '加载中'
							});
							this.changePassword()
							console.log('表单信息：', res)
			            }).catch(err =>{
			                console.log('表单错误信息：', err);
			            })
			        },
					changePassword(){//修改密码
						const id=uni.getStorageSync('user').id
						const token=uni.getStorageSync('token')
						changePassword({...this.formData,id},token).then((re)=>{
							if(re.code==10000){
								uni.removeStorageSync('token')
								uni.navigateTo({
									url:'../login/login'
								})
								setTimeout(() => {
									uni.showToast({title:'修改成功，请重新登录'})
									uni.hideLoading()
								}, 1000)
							}
							setTimeout(() => {
								uni.showToast({title:re.msg})
								uni.hideLoading()
							}, 1000)
						})
					}
		},
		onReady() {
			uni.setNavigationBarTitle({
				title: '修改密码'
			});
			 this.$refs.form.setRules(this.rules)
			 
		},
	}
</script>

<style scoped>
	.form{
		padding:10px;
	}
</style>
